package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    // 客户列表查询
    /***
     * 1. 既要查询客户基本信息，还要查询关联的课程名称，所以需要联查两个表：customer、course
     * 2. 前端传递的多个查询参数，可以封装到一个对象中，比如CustomerDto
     */

    @GetMapping
    public Result list(CustomerDto customerDto) {
        log.info("客户列表查询");
        PageResult<Customer> pageResult = customerService.list( customerDto);
        return Result.success(pageResult);

    }

    // 添加客户
    @PostMapping
    public Result add(Customer customer) {
        log.info("添加客户");
        customerService.add(customer);
        return Result.success();
    }

    // 根据id查询客户
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id) {
        log.info("查询客户: {}", id);
        Customer customer = customerService.getCustomerById(id);
        return Result.success(customer);
    }

    // 修改客户
    @PutMapping
    public Result update(Customer customer) {
        log.info("修改客户: {}", customer);
        customerService.update(customer);
        return Result.success();
    }

    // 删除客户
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除客户: {}", id);
        customerService.delete(id);
        return Result.success();
    }
}
